package com.soft.line.service.impl;

import com.soft.line.entity.LasDepartmentInfo;
import com.soft.line.filter.HttpRequestUtil;
import com.soft.line.filter.UserTokenDto;
import com.soft.line.mapper.CertificateInfoMapper;
import com.soft.line.mapper.ContractInfoMapper;
import com.soft.line.mapper.LasDepartmentInfoMapper;
import com.soft.line.service.StatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private ContractInfoMapper contractInfoMapper;
    @Autowired
    private CertificateInfoMapper certificateInfoMapper;
    @Autowired
    private LasDepartmentInfoMapper lasDepartmentInfoMapper;

    @Override
    public List<Map<String, Object>> statisticsForCertificate(Map<String, Object> map) {
        String x = (String)map.get("x");
        String y = (String)map.get("y");
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        if (y.equals("数量")){
            if(x.equals("年份")){
                resultList = certificateInfoMapper.countNumGroupByYear();
            }else if (x.equals("类型")){
                resultList = certificateInfoMapper.countNumGroupByCertType();
            }
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> countNumGroupByDepThisYear(Map<String, Object> map) {
        return contractInfoMapper.countNumGroupByDepThisYear();
    }

    @Override
    public List<Map<String, Object>> countDepGroupByYear(Map<String, Object> map) {
        String dep = map.get("dep").toString();
        System.out.println("dep:"+dep);
        return contractInfoMapper.countDepGroupByYear(dep);
    }

    @Override
    public List<Map<String, Object>> countNumByTypeYear() {
        return contractInfoMapper.countNumByTypeYear();
    }

    @Override
    public List<Map<String, Object>> amountRankingForContract() {
        return contractInfoMapper.amountRanking();
    }

    @Override
    public List<Map<String, Object>> depRankingForContract() {
        return contractInfoMapper.depRanking();
    }

    @Override
    public List<Map<String, Object>> certTypeRankThisYear() {
        return certificateInfoMapper.certTypeRankThisYear();
    }

    @Override
    public List<Map<String, Object>> sumContractByYear(Map<String, Object> map) {
        return contractInfoMapper.sumContractByYear(map.get("year").toString());
    }

    @Override
    public List<Map<String, Object>> sumContractNatureByYear(Map<String, Object> map) {
        return contractInfoMapper.sumContractNatureByYear(map.get("year").toString());
    }

    @Override
    public List<Map<String, Object>> sumContractNatureByYearAndDep(Map<String, Object> map) {
        List<String> depIdList = new ArrayList<String>();
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if (userToken!=null){
            String roleName = userToken.getUserInfo().getRoleName();
            if (roleName.equals("管理员")||roleName.contains("归档登记员")){
                return contractInfoMapper.sumContractNatureByYearAndDep(map.get("year").toString(),depIdList);
            }else if (roleName.equals("部门经理")){
                LasDepartmentInfo departmentInfo = lasDepartmentInfoMapper.getDepByRole(userToken.getUserInfo().getId());
                departmentInfo = getSupDep(departmentInfo.getId());
                depIdList.add(lasDepartmentInfoMapper.selectSuperiorDepByDepId(departmentInfo.getId()).getContractDepId());
                return contractInfoMapper.sumContractNatureByYearAndDep(map.get("year").toString(),depIdList);
            }else{
                return null;
            }
        }else{
            return null;
        }

    }

    @Override
    public List<Map<String, Object>> countNumGroupByDep() {
        List<String> depIdList = new ArrayList<String>();
        UserTokenDto userToken= HttpRequestUtil.getUserToken();
        if (userToken!=null){
            String roleName = userToken.getUserInfo().getRoleName();
            if (roleName.equals("管理员")||roleName.contains("归档登记员")){
                return contractInfoMapper.countNumGroupByDep(depIdList);
            }else if (roleName.equals("部门经理")){
                LasDepartmentInfo departmentInfo = lasDepartmentInfoMapper.getDepByRole(userToken.getUserInfo().getId());
                departmentInfo = getSupDep(departmentInfo.getId());
                depIdList.add(lasDepartmentInfoMapper.selectSuperiorDepByDepId(departmentInfo.getId()).getContractDepId());
                return contractInfoMapper.countNumGroupByDep(depIdList);
            }else{
                return null;
            }
        }else{
            return null;
        }
    }

    public LasDepartmentInfo getSupDep(String depId) {
        LasDepartmentInfo lasDepartmentInfo = lasDepartmentInfoMapper.selectSupDepByDepId(depId);
        while (lasDepartmentInfo!=null && !lasDepartmentInfo.getSuperiorDepId().equals("0")){
            lasDepartmentInfo = getSupDep(lasDepartmentInfo.getId());
        }
        lasDepartmentInfo = lasDepartmentInfoMapper.selectSuperiorDepByDepId(depId);
        return lasDepartmentInfo;
    }

}
